/**
 * JSK Namespace
 * @namespace JSK
 */
var JSK = JSK || {};

/**
 * @property Test
 * @return {Object} Object containing methods to test status of the app 
 */
JSK.Test = (function() {

    return {

	/**
	 * @method support
	 * @return {Boolean} True if all required features are supported; else false
	 */
	support: (function() {

	    Modernizr.addTest( 'contenteditable', function() {
		return 'isContentEditable' in document.documentElement;
	    });

	    if ( !Modernizr.contenteditable || !Modernizr.applicationcache || !Modernizr.localstorage || !Modernizr.input.placeholder )
		return false;

	    else
		return true;

	}()),

	/**
	 * @method online
	 * @return {Boolean} True if the app is online; else false
	 */
	online: (function() {

    	    return (function() {

		var fnSCb = (function() {

		    return (function( oData ) {

			localStorage[ 'pdfbldr_docid' ] = oData.docid;

			$( '#gen_docid' ).attr( 'value', localStorage[ 'pdfbldr_docid' ] );

			$( '#btn_pdf' ).pdf();
			$( '#btn_get' ).get();

			$( '#logger' ).log({
			    info: 'PDFBldr is online. Synchronized changes to server'
			});

		    });

		}()),

		fnOCb = (function() {

		    return (function( blStat ) {

			localStorage[ 'pdfbldr_content' ] = CKEDITOR.instances.editor.getData();

			if ( blStat && navigator.onLine) {

			    $.ajax({

				url: 'php/set.php',
				type: 'POST',
				dataType: 'JSON',
				data: {
				    docid: localStorage[ 'pdfbldr_docid' ],
				    content: localStorage[ 'pdfbldr_content' ]
				},
				success: fnSCb

			    });

			    $( '#btn_get' ).removeAttr( 'disabled' );
			    $( '#btn_pdf' ).removeAttr( 'disabled' );

			} else {

			    $( '#logger' ).log({
				warn: 'PDFBldr is offline. Synchronized changes to cache'
			    });

			    $( '#btn_get' ).attr( 'disabled', 'disabled' );
			    $( '#btn_pdf' ).attr( 'disabled', 'disabled' );

			}

		    });

		}());

	        $.getJSON( 'php/net.php', fnOCb );

	    });

	}())

    };

}());

/**
 * jQuery Plugins
 */
(function( $ ) {

    /**
     * Plugin to log messages to screen
     */
    $.fn.log = (function() {

	var txtMsg = null, oLoggr = null;

	return (function( oCfg ) {

	    $.each( oCfg, function( strLevel, strMsg ) {

		if ( !oLoggr )
		    oLoggr = document.createElement( 'div' );

		else
		    oLoggr.removeChild( txtMsg );

		txtMsg = document.createTextNode( strMsg );

		switch ( strLevel ) {

		    case 'info':
			oLoggr.setAttribute( 'class', 'alert alert-info' );
			break;

		    case 'warn':
			oLoggr.setAttribute( 'class', 'alert alert-warning' );
			break;

		    case 'error':
			oLoggr.setAttribute( 'class', 'alert alert-danger' );
			break;

		}

		oLoggr.appendChild( txtMsg );

	    });

	    this.append( oLoggr );

	});

    }());

    /**
     * Plugin to generate the PDF
     */
    $.fn.pdf = (function() {

	return (function() {

	    var fnSCb = (function() {

		return (function( oData ) {

		    location.href = oData.file;

		});

	    }()),

	    fnCCb = (function() {

		return (function() {

		    $.ajax({

			url: 'php/pdf.php',
			dataType: 'JSON',
			data: {
			    docid: localStorage[ 'pdfbldr_docid' ]
			},
			success: fnSCb

		    });

		});

	    }());

	    this.on( 'click', fnCCb );

	});

    }());

    /**
     * Plugin to open an existing document
     */
    $.fn.get = (function() {

	return (function() {

	    var fnSCb = (function() {

		return (function( oData ) {

		    if ( !oData.content ) {

			$( '#logger' ).log({
			    error: 'Invalid or non existent Document ID specified'			    
			});

		    } else {

		        CKEDITOR.instances.editor.setData( oData.content );
		        $( '#gen_docid' ).val( oData.docid );

		        localStorage[ 'pdfbldr_docid' ] = oData.docid;
		        localStorage[ 'pdfbldr_saved' ] = oData.saved;

		        $( '#logger' ).log({
			    info: 'Document loaded successfully. Last modified ' + oData.saved
		        });

		    }

		});

	    }()),

	    fnCCb = (function() {

		return (function() {

		    $.ajax({

			url: 'php/get.php',
			dataType: 'JSON',
			data: {
			    docid: $( '#get_docid' ).val()
			},
			success: fnSCb

		    });

		});

	    }());

	    this.on( 'click', fnCCb );

	});

    }());

}( jQuery ));
